﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using PluginCommon;
using WinAPI.User32;

namespace MainProgram
{
    public partial class MainForm : Form
    {
        readonly List<PluginFormBase> PlugIns = new List<PluginFormBase>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            skinEngine.SkinFile = string.Format("{0}\\Skin\\MacOS.ssk", Application.StartupPath);
            string pluginPath = string.Format("{0}\\Plugin", Application.StartupPath);
            string[] pulginFiles = Directory.GetFiles(pluginPath, "*.dll");

            foreach (var file in pulginFiles)
            {
                var assembly = Assembly.LoadFile(file);
                var types = assembly.GetTypes();
                foreach (var type in types)
                {
                    if (type.GetInterface("IPlugin") != null)
                    {
                        var instance = Activator.CreateInstance(type);
                        var u = ((IPlugin)instance).GetPlugin();
                        var tabPage = new TabPage(((IPlugin)instance).GetPlugName());
                        u.RequestCommand += Handle_RequestCommand;
                        tabPage.Controls.Add(u);
                        MainTabControl.TabPages.Add(tabPage);
                        PlugIns.Add(u);
                    }
                }

            }
        }

        private void Handle_RequestCommand(object sender, RequestCommandEventArgs e)
        {
            switch (e.Type)
            {
                case RequestCommandType.SetNotifyIconMessage:
                    TrayNotifyIcon.BalloonTipText = e.Message;
                    TrayNotifyIcon.ShowBalloonTip(5000);
                    break;
            }
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
            }
        }

        private void TrayNotifyIcon_Click(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Show();
                WindowState = FormWindowState.Normal;
            }
            else
            {
                this.BringToFront();
                this.Activate();
            }
        }

        private void MainTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            var pluginForm = ((PluginFormBase)MainTabControl.SelectedTab.Controls[0]);
            pluginForm.FormBorderStyle = FormBorderStyle.None;
            pluginForm.Show();
            pluginForm.WindowState = FormWindowState.Maximized;
        }

        public void Nudge(Form form)
        {
            int xorignal = form.Left;
            int yorignal = form.Top;

            Random random = new Random((int)DateTime.Now.Ticks);

            for (int i = 0; i < 100; i++)
            {
                int xnew = random.Next(xorignal - 15, xorignal + 15);
                int ynew = random.Next(yorignal - 15, yorignal + 15);

                form.Location = new Point(xnew, ynew);
                Thread.Sleep(20);
            }
        }

        private void nudgeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Nudge(this);
        }
    }
}
